package controllers

import (
	"pegasus/datatypes"
	"pegasus/repositories"
	"pegasus/utils"
)

//User 用户注册 查询
type User struct {
	utils.ILogger
}

//RegisterBM 注册用户参数
type RegisterBM struct {
	User     string
	Password string
}

//Register 用户注册
func (user *User) Register(ur *repositories.UserRepo, bm *RegisterBM) *Response {
	if bm.User == "" || bm.Password == "" {
		return NewResponseErr(ErrorArgument, ErrorArgumentMsg)
	}
	if err := ur.Register(bm.User, bm.Password); err != nil {
		return NewResponseErr(ErrorGeneric, err.Error())
	}
	return NewResponseSucc(nil)
}

//UpdatePasswordBM 修改帐号密码
type UpdatePasswordBM struct {
	OldPassword string
	NewPassword string
}

//UpdatePassword 修改密码
func (user *User) UpdatePassword(si datatypes.SIContent, ur *repositories.UserRepo, bm *UpdatePasswordBM) *Response {
	if bm.OldPassword == "" || bm.NewPassword == "" {
		return NewResponseErr(ErrorArgument, ErrorArgumentMsg)
	}
	if err := ur.UpdatePassword(si.User(), bm.OldPassword, bm.NewPassword); err != nil {
		return NewResponseErr(ErrorGeneric, err.Error())
	}
	return NewResponseSucc(nil)
}
